class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        int n=nums.size();
        unordered_map<int,int> has;
        has[0]=1;
        int ret=0;
        int sum=0;
        for(int i=0;i<n;i++)
        {
            sum+=nums[i];
            if(has.count(sum-k))
            {
                ret+=has[sum-k];
            }
            has[sum]++;
        }
        return ret;
    }
};
